// MBDReader - Map Binary Data Format Reader
// Specifically designed for whhx-conqueror.

import java.io.*;

public class MBDReader {
    DataInputStream dis;
    public MBDReader(File f) throws FileNotFoundException {
        dis = new DataInputStream(new FileInputStream(f));
    }
    public MapBinaryData read() throws IOException {
        short strlen = dis.readShort();
        char[] name = new char[strlen];
        for (int i = 0; i < strlen; i++) {
            name[i] = (char) dis.readByte();
        }
        short width = dis.readShort();
        short height = dis.readShort();
        int blocks = width * height;
        short cities = dis.readShort();
        short countries = dis.readShort();
        short factions = dis.readShort();
        MapBinaryData.Block[] blocks_array = new MapBinaryData.Block[blocks];
        for (int i = 0; i < blocks; i++) {
            MapBinaryData.Block.Building[] buildings = new MapBinaryData.Block.Building[5];
            for (int j = 0; j < 5; j++) {
                byte id = dis.readByte();
                byte lvl = dis.readByte();
                buildings[j] = new MapBinaryData.Block.Building(id, lvl);
            }
            short belongCity = dis.readShort();
            byte terrain = dis.readByte();
            blocks_array[i] = new MapBinaryData.Block(buildings, belongCity, terrain);
        }
        MapBinaryData.City[] cities_array = new MapBinaryData.City[cities];
        for (int i = 0; i < cities; i++) {
            short capitalx = dis.readShort();
            short capitaly = dis.readShort();
            short belongCountry = dis.readShort();
            byte name_len = dis.readByte();
            char[] city_name_ca = new char[name_len];
            for (int j = 0; j < name_len; j++) {
                city_name_ca[j] = (char) dis.readByte();
            }
            String city_name = new String(city_name_ca);
            byte lvl = dis.readByte();
            cities_array[i] = new MapBinaryData.City(capitalx, capitaly, belongCountry, city_name, lvl);
        }
        MapBinaryData.Country[] countries_array = new MapBinaryData.Country[countries];
        for (int i = 0; i < countries; i++) {
            byte name_len = dis.readByte();
            char[] country_name_ca = new char[name_len];
            for (int j = 0; j < name_len; j++) {
                country_name_ca[j] = (char) dis.readByte();
            }
            String country_name = new String(country_name_ca);
            short capital = dis.readShort();
            if (cities_array[capital].belongCountry != i) throw new IOException("Capital does not belong to your country"); // TODO: temp solution, replace this
            short faction = dis.readShort();
            byte red = dis.readByte();
            byte green = dis.readByte();
            byte blue = dis.readByte();
            countries_array[i] = new MapBinaryData.Country(country_name, capital, faction, red, green, blue);
        }
        return new MapBinaryData(new String(name), width, height, cities, countries, factions, blocks_array, cities_array, countries_array);
    }
    public void close() throws IOException {
        dis.close();
    }
}